package com.cms.controller.admin.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cms.controller.admin.BaseController;
import com.cms.domain.Feedback;
import com.cms.entity.ModelField;
import com.cms.service.ModelFieldService;
import com.cms.service.ModelService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * Controller - 模型字段
 * 
 * 
 * 
 */
@Controller("adminModelModelFieldController")
@RequestMapping("/admin/model/model_field")
public class ModelFieldController extends BaseController {

	@Autowired
	private ModelService modelService;

	@Autowired
	private ModelFieldService modelFieldService;

	
	/**
	 * 列表
	 */
	@RequestMapping
	public String index(Integer modelId, ModelMap modelMap) {
		setListQuery();
		List<ModelField> modelFields = modelFieldService.findList(modelId);
		modelMap.addAttribute("modelFields", modelFields);
		modelMap.addAttribute("modelId", modelId);
		return getView("model/model_field/index");
	}
    
    /**
     * 检查名称是否存在
     */
	@ResponseBody
	@RequestMapping("/checkName")
    public Boolean checkName(Integer modelId,String name) {
        if (StringUtils.isEmpty(name)) {
            return false;
        }
        return !modelFieldService.nameExists(modelId,name);
    }


	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public String add(Integer modelId,ModelMap modelMap) {
		modelMap.addAttribute("model", modelService.getById(modelId));
		return getView("model/model_field/add");
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public String save(ModelField modelField) {
		modelField.setCreateDate(LocalDateTime.now());
		modelField.setUpdateDate(LocalDateTime.now());
		modelFieldService.save(modelField);
		return "redirect:"+getListQuery("/admin/model/model_field");
	}

	
	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(Integer id,ModelMap modelMap) {
		modelMap.addAttribute("modelField", modelFieldService.getById(id));
		return getView("model/model_field/edit");
	}

	/**
	 * 更新
	 */
	@RequestMapping("/update")
	public String update(ModelField modelField) {
		modelField.setUpdateDate(LocalDateTime.now());
		modelFieldService.updateById(modelField);
		return "redirect:"+getListQuery("/admin/model/model_field");
	}
	
   /**
     * 修改排序
     */
   	@ResponseBody
   	@RequestMapping("/updateSort")
    public Feedback updateSort(String sortArray){
        JSONArray jsonArray = JSONArray.parseArray(sortArray);
        for(int i=0;i<jsonArray.size();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
			Integer id = jsonObject.getInteger("id");
            Integer sort = jsonObject.getInteger("sort");
            ModelField modelField = modelFieldService.getById(id);
            modelField.setSort(sort);
			modelFieldService.updateById(modelField);
        }
        return Feedback.success(new HashMap<>());
    }

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Feedback delete(Integer[] ids) {
		if(ArrayUtils.isNotEmpty(ids)){
			modelFieldService.removeByIds(Arrays.asList(ids));
		}
		return Feedback.success(new HashMap<>());
	}

}